package com.ce.cases.check.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.cases.check.dao.po.CaseCheckEnterprice;
import com.ce.cases.check.dao.po.CaseEnterpriceInfo;
import com.ce.framework.datamaintain.dao.IBaseDao;
import com.ce.framework.exception.DaoException;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.StringUtil;

public class CaseCheckLogic implements ICaseCheckLogic {
	private IBaseDao basedao;
	
	public void dealEnterpriceInfo(CaseCheckEnterprice enterprice,
			String[] investors, Double[] investmentsRate,
			Integer[] year, Double[] mainIncome,Double[] mainCost,Double[] addedTax,Double[] incomeTax,
			Date[] dealDate, String[] illegalResult,Double[]overdueTax,Double[] fine){
			try {
				//修改检查对象基本情况
				if(StringUtil.isNotBlank(enterprice.getVcId())){
				   this.basedao.updatePo(enterprice);
				   //删除检查对象附加信息进行重新添加
				   this.basedao.deleteSearchHQL("From CaseEnterpriceInfo info where info.checkEntid='"+enterprice.getVcId()+"'");
				}else{
					enterprice.setVcId(GeneratorUUID.generateUUID());
					this.basedao.addPo(enterprice);
				}
				//添加企业附加信息分别添加“投资组成”、“经营状况”、“违法记录”
				List<CaseEnterpriceInfo> infoList=new ArrayList<CaseEnterpriceInfo>();
				CaseEnterpriceInfo info=null;
				  if(investors!=null){
					  for(int i=0;i<investors.length;i++){
						  info=new CaseEnterpriceInfo();
						  info.setVcId(GeneratorUUID.generateUUID());
						  info.setCheckEntid(enterprice.getVcId());
						  info.setInvestors(investors[i]);
						  info.setInvestmentsRate(investmentsRate[i]);
						  info.setFlag(1);
						  infoList.add(info);
					  }
				  }
				  if(year!=null){
					  for(int i=0;i<year.length;i++){
						  info=new CaseEnterpriceInfo();
						  info.setVcId(GeneratorUUID.generateUUID());
						  info.setCheckEntid(enterprice.getVcId());
						  info.setYear(year[i]);
						  info.setMainIncome(mainIncome[i]);
						  info.setMainCost(mainCost[i]);
						  info.setAddedTax(addedTax[i]);
						  info.setIncomeTax(incomeTax[i]);
						  info.setFlag(2);
						  infoList.add(info);
					  } 
				  }
				 
				  if(dealDate!=null){
					  for(int i=0;i<dealDate.length;i++){
						  info=new CaseEnterpriceInfo();
						  info.setVcId(GeneratorUUID.generateUUID());
						  info.setCheckEntid(enterprice.getVcId());
						  info.setDealDate(dealDate[i]);
						  info.setIllegalResult(illegalResult[i]);
						  info.setOverdueTax(overdueTax[i]);
						  info.setFine(fine[i]);
						  info.setFlag(3);
						  infoList.add(info);
					  }
				  }
				  if(!infoList.isEmpty())
					  this.basedao.addPos(infoList);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	@SuppressWarnings("unchecked")
	public List<CaseEnterpriceInfo> getCaseEnterpriceInfoByFlag(
			String checkEntId, int flag) {
		try {
			return (List<CaseEnterpriceInfo>) this.basedao
					.findByHql("From CaseEnterpriceInfo po where po.checkEntid='"
							+ checkEntId + "' and po.flag=" + flag + "");
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return null;
	}

	public IBaseDao getBasedao() {
		return basedao;
	}

	public void setBasedao(IBaseDao basedao) {
		this.basedao = basedao;
	}
	
	
}
